{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-27T10:33:22.806211Z",
     "start_time": "2024-07-27T10:30:42.939365Z"
    }
   },
   "source": [
    "# 从xlsx中读取数据\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from glob import glob\n",
    "\n",
    "data_folder=r'G:\\PAPER3\\merge_data_csv'\n",
    "\n",
    "# 使用2001-2015年的数据作为训练数据\n",
    "train_year=list(range(2001,2016))\n",
    "test_year=list(range(2016,2021))\n",
    "\n",
    "# 读取训练数据\n",
    "train_files=[]\n",
    "for year in train_year:\n",
    "    train_files.extend(glob(data_folder+'\\\\'+str(year)+'*.xlsx'))\n",
    "\n",
    "# 读取测试数据\n",
    "test_files=[]\n",
    "for year in test_year:\n",
    "    test_files.extend(glob(data_folder+'\\\\'+str(year)+'*.xlsx'))\n",
    "\n",
    "# 使用pandas读取数据并合并训练数据\n",
    "train_data=pd.DataFrame()\n",
    "for file in train_files:\n",
    "    df=pd.read_excel(file)\n",
    "    train_data=pd.concat([train_data,df],ignore_index=True)\n",
    "    train_data=train_data.dropna()\n",
    "    train_data=train_data.reset_index(drop=True)\n",
    "\n",
    "train_data.head()\n",
    "\n",
    "# 保存训练数据\n",
    "train_data.to_csv(r'G:\\PAPER3\\train_data'+'\\\\'+'train_data.csv',index=False)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   GOSIF  dem      aspect        EVI  LAI  LST_Day     LST_Night      NDVI  \\\n",
       "0    -86  876   79.193680   9.499997  738     1.25  13676.750000  13291.00   \n",
       "1    -81  371  201.005096   1.817600  254     1.00  13648.000000  13290.00   \n",
       "2    -42  448  354.173645   5.276418  750     2.00  13655.666667  13267.75   \n",
       "3    -46  259   85.383644   3.466256  565     0.75  13683.666667  13354.00   \n",
       "4   -104  748  157.193512  10.861674  762     1.25  13767.750000  13314.75   \n",
       "\n",
       "    Nir      Red    slope  \n",
       "0  1729  1406.25  1065.75  \n",
       "1   290  2842.00  2479.50  \n",
       "2  1323  1778.00  1645.50  \n",
       "3  1138  3040.50  2762.25  \n",
       "4  1654  1879.75  1451.25  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GOSIF</th>\n",
       "      <th>dem</th>\n",
       "      <th>aspect</th>\n",
       "      <th>EVI</th>\n",
       "      <th>LAI</th>\n",
       "      <th>LST_Day</th>\n",
       "      <th>LST_Night</th>\n",
       "      <th>NDVI</th>\n",
       "      <th>Nir</th>\n",
       "      <th>Red</th>\n",
       "      <th>slope</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-86</td>\n",
       "      <td>876</td>\n",
       "      <td>79.193680</td>\n",
       "      <td>9.499997</td>\n",
       "      <td>738</td>\n",
       "      <td>1.25</td>\n",
       "      <td>13676.750000</td>\n",
       "      <td>13291.00</td>\n",
       "      <td>1729</td>\n",
       "      <td>1406.25</td>\n",
       "      <td>1065.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-81</td>\n",
       "      <td>371</td>\n",
       "      <td>201.005096</td>\n",
       "      <td>1.817600</td>\n",
       "      <td>254</td>\n",
       "      <td>1.00</td>\n",
       "      <td>13648.000000</td>\n",
       "      <td>13290.00</td>\n",
       "      <td>290</td>\n",
       "      <td>2842.00</td>\n",
       "      <td>2479.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-42</td>\n",
       "      <td>448</td>\n",
       "      <td>354.173645</td>\n",
       "      <td>5.276418</td>\n",
       "      <td>750</td>\n",
       "      <td>2.00</td>\n",
       "      <td>13655.666667</td>\n",
       "      <td>13267.75</td>\n",
       "      <td>1323</td>\n",
       "      <td>1778.00</td>\n",
       "      <td>1645.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-46</td>\n",
       "      <td>259</td>\n",
       "      <td>85.383644</td>\n",
       "      <td>3.466256</td>\n",
       "      <td>565</td>\n",
       "      <td>0.75</td>\n",
       "      <td>13683.666667</td>\n",
       "      <td>13354.00</td>\n",
       "      <td>1138</td>\n",
       "      <td>3040.50</td>\n",
       "      <td>2762.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-104</td>\n",
       "      <td>748</td>\n",
       "      <td>157.193512</td>\n",
       "      <td>10.861674</td>\n",
       "      <td>762</td>\n",
       "      <td>1.25</td>\n",
       "      <td>13767.750000</td>\n",
       "      <td>13314.75</td>\n",
       "      <td>1654</td>\n",
       "      <td>1879.75</td>\n",
       "      <td>1451.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-27T10:37:09.202258Z",
     "start_time": "2024-07-27T10:36:02.805570Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用pandas读取数据并合并测试数据\n",
    "test_data=pd.DataFrame()\n",
    "for file in test_files:\n",
    "    df=pd.read_excel(file)\n",
    "    test_data=pd.concat([test_data,df],ignore_index=True)\n",
    "    test_data=test_data.dropna()\n",
    "    test_data=test_data.reset_index(drop=True)\n",
    "# 保存测试数据\n",
    "test_data.to_csv(r'G:\\PAPER3\\train_data'+'\\\\'+'test_data.csv',index=False)"
   ],
   "id": "37001131368796d",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "a4e1d164428f5f5f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
